package com.desaysv.hicar;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.desaysv.svview.webkit.SVWebView;


public class WebActivity extends AppCompatActivity {

    private NotificationManager notificationManager;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(getPackageName(), "通知", NotificationManager.IMPORTANCE_MIN);
            notificationManager.createNotificationChannel(channel);
        }

        SVWebView web = findViewById(R.id.web);
        web.setSvWebViewClient(new SVWebView.Companion.SvWebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
//        web.loadUrl("http://linziblog.cn");
        web.addJavascriptInterface(this, "Android");
        web.loadUrl("file:android_asset/web/test.html");
        web.postDelayed(new Runnable() {
            @Override
            public void run() {
                test2("123456");
            }
        }, 5000);
    }

    @SVWebView.Js
    public String test(String info, String info2) {
        Toast.makeText(this, info2, Toast.LENGTH_SHORT).show();

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            Notification.Builder builder = new Notification.Builder(this, getPackageName());
            builder.setContentTitle("123456")
                    .setContentText("1234")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setAutoCancel(false);
            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_NO_CLEAR;
            notificationManager.notify(1, notification);
        }
        return "1234";
    }

    @SVWebView.Js
    public String test2(String info) {
        Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification.Builder builder = new Notification.Builder(this, getPackageName());
            builder.setContentTitle("123456222222")
                    .setContentText("12346666")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setAutoCancel(false);
            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_NO_CLEAR;
            notificationManager.notify(1, notification);
        }
        launcher();
        return "12345";
    }

    public void launcher() {
        Intent intent = new Intent();
        intent.setClassName(getPackageName(), "com.desaysv.hicar.MainActivity2");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
